import { CommonUtil } from "../Common/Util/CommonUtil";

const { ccclass, property } = cc._decorator;

@ccclass("PoolManager")
export class PoolManager {
    public static handle = new Map<string, cc.Node[]>();

    /**
     * 获取缓存资源节点
     * @param prefab 需要查找的资源
     * @param parent 父节点
     * @param recycleList 回收列表
     * @returns 查到的可用节点
     */
    public static getNode(prefab: cc.Prefab, parent: cc.Node, recycleList: cc.Node[]) {
        const name = prefab.data.name;
        let node: cc.Node = null;
        const pool = this.handle.get(name);
        if (this.handle.has(name) && pool.length > 0) {
            node = this.handle.get(name).pop();
            // 是否有相同uuid
            let hasResult: boolean = false;
            for (let cell of recycleList) {
                if (cell.uuid == node.uuid) {
                    hasResult = true;
                }
            }
            if (!hasResult) {
                recycleList.push(node);
            }
            cc.log('从缓存池子中获取', CommonUtil.formatStringLen(name), ' uuid =', node.uuid, 'pool.length =', pool.length);
        } else {
            node = cc.instantiate(prefab);
            if (recycleList) {
                recycleList.push(node);
                cc.log('新创键', CommonUtil.formatStringLen(name), ' uuid =', node.uuid);
            }
        }

        // TODO
        // 如果父节点的根节点存在Button组件
        // 会存在
        // 先设置父节点后 再执行node属性的改变会不生效
        node.parent = parent;
        return node;
    }

    /** 回收 ♻️ */
    public static setNode(target: cc.Node) {
        const name = target.name;
        target.parent = null;
        if (this.handle.has(name)) {
            let mapIterator = this.handle.values();
            let hasResult: boolean = false;
            let i = 0;
            while (i < this.handle.size) {
                let ffff = mapIterator.next().value;
                for (let m of ffff) {
                    if (m.uuid == target.uuid) {
                        hasResult = true;
                    }
                }
                i++;
            }
            if (!hasResult) {
                this.handle.get(name).push(target);
                // const pool = this.handle.get(name);
                // cc.log('放入池子', name, 'uuid =', target.uuid, 'pool.length =', pool.length);
            }
            else {
                // cc.log('不放入池子');
            }
        } else {
            if (name.length > 0) {
                this.handle.set(name, [target]);
                const pool = this.handle.get(name);
                // cc.log('创建新池子', name, 'uuid =', target.uuid, 'pool.length =', pool.length);
            } else {
                target.destroy();
            }
        }
    }

    /** 回收所有节点 */
    public static recycleByList(recycleList: cc.Node[]) {
        if (recycleList == null) { return; }

        for (let item of recycleList) {
            if (item.name == 'CommonItem') {
                item.getComponent(item.name).reset();
            }
            this.setNode(item);
        }
    }
}